package EngineView;

use strict;
use CGI qw(:standard);


use Athena::WebAPI::HTML;
use Athena::WebAPI::Form;
use Athena::WebAPI::Table;
use Athena::WebAPI::View;

use Athena::NavHeaderView;
use Athena::CellFormat qw(:all);

use base ("View");

sub new {

    # Get the class name
    my $stInvocant = shift(@_);
    my $stClass = ref($stInvocant) || $stInvocant;

    # Create the current instance and bless into class
    my $rSelf = { };
    bless($rSelf,$stClass);

    my %hParams = ( @_ );
    $rSelf->{params} = \%hParams;
    $rSelf->cellFormatInit();
    return $rSelf;
}

sub draw {
  my $rSelf = shift(@_);
  my $rParams = $rSelf->{params};

  my $rTable = Table->new(%{$rSelf->{params}->{table_style}});
  $rSelf->buildTable($rTable);

  my $rForm = Form->new(-method=>'post', -name=>'myForm');
  $rForm->addChild($rTable);
  $rForm->addHiddenFields($rParams->{'hidden_fields'});

  my $rHTML = HTML->new();
  $rHTML->setBodyAttr(%{$rSelf->{params}->{body_style}});
  $rHTML->addChild($rForm);
  
  return $rHTML->draw();
}

sub buildTable {
   my $rSelf = shift(@_);
   my $rTable = shift(@_);
   my $iCurRow = 0;
   $rTable->addChild($iCurRow++, 0, NavHeaderView->new($rSelf->{params}));
   $rTable->addChild($iCurRow++, 0, "View Engine Stats",-bgcolor=>"#ddcccc");

   my $rEngineTable = Table->new();
   my $rTableLeft = Table->new(-border=>'1', -cellpadding=>'3');
   my $rTableMiddle = Table->new(-border=>'1', -cellpadding=>'3');
   my $rTableRight = Table->new(-border=>'1', -cellpadding=>'3');
   my $key;
   my $iLCurRow = 0;
   my $iMCurRow = 0;
   my $iRCurRow = 0;
   my $stRowColor;

   $rTableLeft->addChild($iLCurRow, 0, "engine level", -colspan=>2);
   my $rStats = $rSelf->{params}->{engine}->{stats};
   foreach $key (keys(%{$rStats})) {
     if (($key eq "phys") || ($key eq "farm")){
       next;
     }
     else {
       $iLCurRow++;
       $stRowColor = $rSelf->colColor();
       $rTableLeft->addChild($iLCurRow, 0, $key, -bgcolor=>$stRowColor);
       $rTableLeft->addChild($iLCurRow, 1, " " . $rStats->{$key} . " ", 
			       -bgcolor=>$stRowColor);
     }
   }

   $iMCurRow++;
   $rTableMiddle->addChild($iMCurRow, 0, "to physicals", -colspan=>2);
   my $pkey;
   foreach $pkey (sort(keys(%{$rStats->{phys}}))) {
     $iMCurRow++;
     $stRowColor = $rSelf->colColor();
     $rTableMiddle->addChild($iMCurRow, 0, $pkey, -bgcolor=>$stRowColor,
			    -align=>'right');
     $rTableMiddle->addChild($iMCurRow, 1, " ".$rStats->{phys}->{$pkey}." ", 
			    -bgcolor=>$stRowColor);
   }
   $iRCurRow++;
   $rTableRight->addChild($iRCurRow, 0, "to farms", -colspan=>2);
   my $fkey;
   foreach $fkey (sort(keys(%{$rStats->{farm}}))) {
     $iRCurRow++;
     $stRowColor = $rSelf->colColor();
     $rTableRight->addChild($iRCurRow, 0, $fkey, -bgcolor=>$stRowColor, 
			    -align=>'right');
     $rTableRight->addChild($iRCurRow, 1, 
			    " ".$rStats->{farm}->{$fkey}." ", 
			    -bgcolor=>$stRowColor);
   }
   $rEngineTable->addChild(0, 0, $rTableLeft, -valign=>'top');
   $rEngineTable->addChild(0, 1, $rTableMiddle, -valign=>'top');
   $rEngineTable->addChild(0, 2, $rTableRight, -valign=>'top');
   $rTable->addChild($iCurRow++, 0, $rEngineTable);
   $rTable->addChild($iCurRow++, 0, submit(-name=>'send'), 
		     -bgcolor=>'#ddcccc');
   $rTable->addChild($iCurRow++, 0, NavFooterView->new($rSelf->{params}));
}

1;
